/**
 * @author		Ben Layet
 * @version 	0.1
 * @date		2 Sep 2008
 */
package org.learnaholic.application.ui.swing;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.KeyStroke;

import org.learnaholic.application.LearnaholicImpl;
import org.learnaholic.application.model.MemoryItem;
import org.learnaholic.application.model.MemoryItems;
import org.learnaholic.application.ui.LearnaholicUi;
import org.learnaholic.application.ui.UiConstants;
import org.learnaholic.ui.itemtable.LabelsTable;
import org.learnaholic.ui.itemtable.LabelsTableModel;
import org.learnaholic.ui.itemtable.LabelsTableModelEvent;
import org.learnaholic.ui.itemtable.LabelsTableModelListener;
import org.learnaholic.ui.itemtable.TableCell;

/**
 * Main frame of the application.
 */
public class LearnaholicFrame extends JFrame
{
	/** The title */
	private static final String title = "Learnaholic";

	private final LearnaholicPanel learnaholicPanel;
	private final LabelsTableModel tableModel;
	
	/**
	 * Constructor.
	 */
	public LearnaholicFrame()
	{
		super(title);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100,100,1200, 800);
		tableModel = new LabelsTableModel();
		learnaholicPanel = new LearnaholicPanel(tableModel);
        add(learnaholicPanel, BorderLayout.CENTER);
        
      //Where the GUI is created:
        JMenuBar menuBar;
        JMenu menu, submenu;
        JMenuItem menuItem;
        JRadioButtonMenuItem rbMenuItem;
        JCheckBoxMenuItem cbMenuItem;

        //Create the menu bar.
        menuBar = new JMenuBar();

        //Build the first menu.
        menu = new JMenu("File");
        menu.setMnemonic(KeyEvent.VK_F);
        menu.getAccessibleContext().setAccessibleDescription(
                "File Menu");
        menuBar.add(menu);

        //a group of JMenuItems
        menuItem = new JMenuItem("Save", KeyEvent.VK_S);
        menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
        menuItem.getAccessibleContext().setAccessibleDescription(
                "Save the result of the current test.");
        menu.add(menuItem);
        

        menuItem.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent e)
			{
				LearnaholicImpl.getInstance().save();
			}});
        
        menuItem = new JMenuItem("Open List", KeyEvent.VK_O);
        menuItem.getAccessibleContext().setAccessibleDescription(
                "Open a new list.");
        menu.add(menuItem);
        menuItem = new JMenuItem("Import Results", KeyEvent.VK_I);
        menuItem.getAccessibleContext().setAccessibleDescription(
                "Import the results of a previous test.");
        menu.add(menuItem);

        menuItem = new JMenuItem("Exit", KeyEvent.VK_X);
        menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, ActionEvent.CTRL_MASK));
        menuItem.getAccessibleContext().setAccessibleDescription(
                "Exit from the application.");
        menu.add(menuItem);

        menuItem.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent e)
			{
				LearnaholicImpl.getInstance().stop();
				
			}});
        
        //Build the first menu.
        menu = new JMenu("Edit");
        menu.setMnemonic(KeyEvent.VK_E);
        menu.getAccessibleContext().setAccessibleDescription(
                "Edit Menu");
        menuBar.add(menu);

        //a group of JMenuItems
        menuItem = new JMenuItem("Select all", KeyEvent.VK_A);
        menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_A, ActionEvent.CTRL_MASK));
        menuItem.getAccessibleContext().setAccessibleDescription(
                "Select all.");
        menu.add(menuItem);
        menuItem.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent e)
			{
				tableModel.setAllSelected(true);
			}});

        menuItem = new JMenuItem("Unselect all", KeyEvent.VK_U);
        menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D, ActionEvent.CTRL_MASK));
        menuItem.getAccessibleContext().setAccessibleDescription(
                "Unselect all.");
        menu.add(menuItem);
        menuItem.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent e)
			{
				tableModel.setAllSelected(false);
			}});

        //Build the first menu.
        menu = new JMenu("Action");
        menu.setMnemonic(KeyEvent.VK_A);
        menu.getAccessibleContext().setAccessibleDescription(
                "Actions Menu");
        menuBar.add(menu);
        menuItem = new JMenuItem("Start Test", KeyEvent.VK_S);
        menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_T, ActionEvent.CTRL_MASK));
        menuItem.getAccessibleContext().setAccessibleDescription(
                "Start a new test.");
        menu.add(menuItem);

        menuItem.addActionListener(new ActionListener(){

			public void actionPerformed(ActionEvent e)
			{
				LearnaholicImpl.getInstance().startTest(tableModel.getSelected());
				
			}});
        
        
        add(menuBar, BorderLayout.NORTH);
	}
	/**
	 * @param blurred
	 */
	public void setBlurred(boolean blurred)
	{
		String blurredStr = blurred?" *":"";
		super.setTitle(title + blurredStr);
		tableModel.setAllSelected(false);
	}
	/**
	 * @return
	 */
	public LearnaholicUi getPanel()
	{
		return learnaholicPanel;
	}
	




}
